﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Card.Security;

namespace Card.Web.Member
{
    public partial class NameCard : MemberBase
    {
        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DoInit();
        }

        protected override void OnInit(EventArgs e)
        {
            this.rptList.ItemCommand += new RepeaterCommandEventHandler(rptList_ItemCommand);
            this.pager.BuildingUrl += new BAF.WebControls.BuildingUrlHander(pager_BuildingUrl);
            base.OnInit(e);
        }

        void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (string.Compare(e.CommandName, "DELETE") == 0)
            {
                int cardID = 0;
                if (int.TryParse(e.CommandArgument.ToString(), out cardID))
                {
                    BLL.UserCardManager.Delete(cardID,this.Passport);
                    BLL.UserCardFieldManager.DeleteByCardID(cardID, this.Passport);
                    BLL.UserCardLogoManager.DeleteByCardID(cardID, this.Passport);
                    this.DeleteFile(cardID);
                    this.DoInit();
                }
            }
        }

        string pager_BuildingUrl(int index)
        {
            return string.Format("NameCard.aspx?i={0}", index);
        }
        #endregion
        
        #region
        private void DoInit()
        {
            Card.DataBase.Pagination pagination = new Card.DataBase.Pagination()
            {
                TableName = "USER_CARD",
                PageSize = this.pager.PageSize,
                OrderBy = "CREATE_DATE DESC,TEMPLATE_NAME,ID",
                PageNumber = this.pager.PageIndex + 1,
                Condition = this.GetQueryCondition()
            };
            
            DataTable dtData = BLL.TemplateManager.Query(pagination);
            this.pager.RecordCount = pagination.RecordCount;
            if (this.pager.PageCount < pagination.PageNumber)
            {
                pagination.PageNumber = this.pager.PageCount;
                dtData = BLL.TemplateManager.Query(pagination);
                this.pager.RecordCount = pagination.RecordCount;
            }

            this.rptList.DataSource = dtData;
            this.rptList.DataBind();
        }

        private string GetQueryCondition()
        {
            return string.Format(" AND USER_ID={0} ",this.Passport.UserID);
        }

        private bool DeleteFile(int cardID)
        {
            string url = string.Format("{0}/ImageService.ashx?a=du&id={1}", Card.Utility.ImageUtilty.ImageServer, cardID);
            WebRequest request = HttpWebRequest.Create(url);
            request.Method = "GET";
            request.Headers.Add("Cookie", string.Format("{0}={1}", Card.CONST.CookieNames.Passport, CookieManager.GetCookie(Card.CONST.CookieNames.Passport)));

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (StreamReader dataStream = new StreamReader(response.GetResponseStream()))
            {
                var result = dataStream.ReadToEnd();
                JObject jResult = (JObject)JsonConvert.DeserializeObject(result);
                if (string.Compare(jResult.Property("r").Value.ToString(), CONST.YES_NO.YES, true) == 0)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion


    }
}
